<template>
	<view class="page">
		<view class="tabBox">
			<view class="tabBtn" :class="{active:tabType===1}" @click="changeTab(1)">定期专区</view>
			<view class="tabBtn" :class="{active:tabType===2}" @click="changeTab(2)">转让专区</view>
		</view>
		<view class="listBox">
			<scroll-view class="list" scroll-y="true" refresher-enabled="true" :refresher-triggered="triggered" :scroll-top="scrollTop"
			 :refresher-threshold="40" refresher-background="#f9f9f8" @refresherpulling="onPulling" @refresherrefresh="onRefresh"
			 @refresherrestore="onRestore" @refresherabort="onAbort" @scrolltolower="onLoadMore" @scroll="scroll">
				<view class="list-item" v-for="(item,index) in productList" :key="index">
					<view class="top flex">
						<text class="name">{{item.productName}}</text>
						<navigator :url="`/pages/prd-detail/prd-detail?productCodeNew=${item.productCodeNew}`" class="btn">立即投资</navigator>
					</view>
					<view class="bottom flex">
						<view class="rate flex-col">
							<text>{{toFixed(item.annualrate)}}%</text>
							<text class="AAA">预计年化收益率</text>
						</view>
						<view class="days flex-col">
							<text>{{item.deadLine + item.deadLineUnitText}}</text>
							<text class="AAA">投资期限</text>
						</view>
						<view class="amount flex-col">
							<text>{{item.subStartPerson}}元</text>
							<text class="AAA">起投金额</text>
						</view>
					</view>
				</view>
				<uni-load-more :status="more" iconType="auto"></uni-load-more>
			</scroll-view>
			<uni-transition :duration="500" :mode-class="['fade']" :show="oldScrollTop>50">
				<view class="toTop" @click="goTop">
					<uni-icons type="navigate-filled" size="30" />
				</view>
			</uni-transition>

		</view>
	</view>
</template>

<script>
	import {
		uniLoadMore,
		uniIcons,
		uniTransition
	} from '@dcloudio/uni-ui'
	export default {
		data() {
			return {
				tabType: 1,
				triggered: true,
				pageSize: 10,
				pageNumber: 1,
				productList: [],
				more: 'more',
				_freshing: false,
				_loadingMore: false,
				scrollTop: 0,
				oldScrollTop: 0
			};
		},
		components: {
			uniLoadMore,
			uniIcons,
			uniTransition
		},
		onLoad() {
			this.onRefresh();
		},
		methods: {
			changeTab(type) {
				this.tabType = type;
			},
			getProductList() {
				return this.$http.post('kingdom.ktrade.get_quick_pif_bonds_sub_list', 'v4.0', {
					pageNumber: this.pageNumber,
					pageSize: this.pageSize,
					selColumn: "",
					sort: 'asc',
				})
			},
			onPulling(e) {
				// console.log("onpulling", e);
			},
			onRefresh() {
				console.log('onRefresh')
				if (this._loadingMore || this._freshing) return;
				this._freshing = true;
				this.pageNumber = 1;
				this.getProductList().then(data => {
					console.log(data)
					this.productList = data.items;
					this.triggered = false;
					this._freshing = false;
				})
			},
			onLoadMore() {
				if (this._loadingMore || this._freshing || this.more === "none") return;
				this._loadingMore = true;
				this.more = "loading";
				this.pageNumber++;
				this.getProductList().then(data => {
					this.productList = this.productList.concat(data.items);
					this._loadingMore = false;
					if (this.productList.length >= data.lengths) {
						this.more = "none";
					} else {
						this.more = "more";
					}
				})
			},
			onRestore() {
				this.triggered = 'restore'; // 需要重置
				console.log("onRestore");
			},
			onAbort() {
				console.log("onAbort");
			},
			toFixed(value) {
				return Number(value).toFixed(2)
			},
			scroll(e) {
				this.oldScrollTop = e.detail.scrollTop
			},
			goTop: function(e) {
				// 解决view层不同步的问题
				this.scrollTop = this.oldScrollTop;
				this.$nextTick(function() {
					this.scrollTop = 0
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.tabBox {
		display: flex;
		justify-content: space-around;
		align-items: center;
		font-size: 26rpx;
		background-color: #FFFFFF;
		// background-color: rgb(248, 248, 248);

		.tabBtn {
			height: 70rpx;
			line-height: 70rpx;
			padding: 0 10rpx;
			border-bottom: 2px solid #FFFFFF;
		}

		.active {
			color: #f54d4f;
			border-color: #f54d4f;
		}
	}

	.page {
		background-color: #f9f9f8;
	}

	.listBox {
		padding-top: 20rpx;
		height: calc(100vh - 50px - 70rpx);
		box-sizing: border-box;
	}

	/*  #ifdef  MP-WEIXIN  */
	.listBox {
		height: calc(100vh - 70rpx);
	}

	/*  #endif  */

	.toTop {
		position: fixed;
		right: 50rpx;
		bottom: 100px;
	}

	.list {
		height: 100%;

		.list-item {
			color: #333333;
			padding: 20rpx 30rpx;
			background-color: #FFFFFF;
			margin-bottom: 20rpx;

			.flex {
				display: flex;
				justify-content: space-between;
			}

			.flex-col {
				display: flex;
				flex-direction: column;
				justify-content: flex-end;
			}

			.top {
				align-items: center;
				margin-bottom: 40rpx;

				.name {
					font-size: 30rpx;
				}

				.btn {
					font-size: 20rpx;
					padding: 5rpx 10rpx;
					border: 1px solid #f5a67c;
					color: #f5a67c;
					border-radius: 2px;
				}
			}

			.bottom {
				font-size: 28rpx;

				.AAA {
					font-size: 18rpx;
					color: #aaaaaa;
				}

				.rate {
					font-size: 56rpx;
					color: #f54d4f;
				}
			}
		}
	}

	.hide {
		display: none !important;
	}
</style>
